# -*- coding: utf-8 -*-


class Pagination(object):
    
    def __init__(self, current_page, all_count, base_url, query_params, per_page=30, pager_page_count=11):
        """
        :param current_page: 当前页的页码
        :param all_count: 总共有多少条数据
        :param base_url: 基地址
        :param query_params: 查询的携带的参数
        :param per_page: 每页多少条
        :param pager_page_count:在界面显示多少个页码
        """
        self.base_url = base_url
        try:
            self.current_page = int(current_page)
            if self.current_page <= 0:
                self.current_page = 1
        except Exception as e:
            self.current_page = 1
            
        query_params = query_params.copy()
        query_params._mutable = True
        
        self.query_params = query_params
        self.all_count = all_count
        self.per_page = per_page
        self.pager_page_count = pager_page_count
        
        pager_count, b = divmod(all_count, per_page)
        if b != 0:
            pager_count += 1
        self.pager_count = pager_count
        half_pager_page_count = int(pager_page_count / 2)
        self.half_pager_page_count = half_pager_page_count

    @property
    def start(self):
        """数据获取的起始索引"""
        return (self.current_page - 1) * self.per_page
    
    @property
    def end(self):
        """数据获取的结束索引"""
        return self.current_page * self.per_page
    
    def page_html(self):
        
        # 没数据直接返回
        if self.all_count == 0:
            return '<h5>暂无数据</h5>'
        
        # 总页码：小于11，则是多少页就多少页
        if self.pager_count < self.pager_page_count:
            pager_start = 1
            pager_end = self.pager_count
        else:
            # 总页码打于11，看当前页面是否<=11/5，即小于等于5
            if self.current_page <= self.half_pager_page_count:
                pager_start = 1
                pager_end = self.pager_page_count
            else:
                # 当前页码>=5，看当前页码加上5 是否大于数据分页后的总页码
                if (self.current_page + self.half_pager_page_count) > self.pager_count:
                    pager_end = self.pager_count
                    pager_start = self.pager_count - self.pager_page_count + 1
                else:
                    pager_start = self.current_page - self.half_pager_page_count
                    pager_end = self.current_page + self.half_pager_page_count
        
        page_list = []
        if self.current_page <= 1:
            prev = '<li><a href="#">上一页</a></li>'
        else:
            self.query_params['page'] = self.current_page - 1
            prev = '<li><a href="%s?%s">上一页</a></li>' % (self.base_url, self.query_params.urlencode())
        page_list.append(prev)
        
        for i in range(pager_start, pager_end + 1):
            self.query_params['page'] = i
            if self.current_page == i:
                tpl = '<li class="active"><a href="%s?%s">%s</a></li>' % (
                    self.base_url, self.query_params.urlencode(), i)
            else:
                tpl = '<li><a href="%s?%s">%s</a></li>' % (self.base_url, self.query_params.urlencode(), i)
            page_list.append(tpl)
        
        if self.current_page >= self.pager_count:
            nex = '<li><a href="#">下一页</a></li>'
        else:
            self.query_params['page'] = self.current_page + 1
            nex = '<li><a href="%s?%s">下一页</a></li>' % (self.base_url, self.query_params.urlencode())
        page_list.append(nex)
        
        if self.all_count:
            tpl = '<li class="disabled"><a>共%s条数据，页码%s/%s</a></li>' % (
            self.all_count, self.current_page, self.pager_count)
        
        page_str = ''.join(page_list)
        return page_str
